package com.showcai.controller.apply;

/**
 * Created by Baggio on 2017/1/6.
 */

import com.showcai.controller.base.WeChatBaseController;
import com.showcai.module.*;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.redis.RedisCache;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.ApplyService;
import com.showcai.service.DistributorService;
import com.showcai.service.OrderService;
import com.showcai.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RequestMapping(value = "/apply")
@Controller
public class ApplyContorller extends WeChatBaseController {
    @Autowired
    RedisCache redisCache;
    private static final Long PIC_SIZE = 500l * 1024;

    @Autowired
    private UserService userService;
    @Autowired
    OrderService orderService;

    @Autowired
    ApplyService applyWatchService;

    @Autowired
    DistributorService distributorService;

    @RequestMapping(value = "/apply")
    public ModelAndView apply(String distributorCode) {
        logger.info("apply==================================>>>>>>" + super.session.getId());
        ModelAndView mov = new ModelAndView();
        String views = "apply/web/apply";
        if (getIsMobile()) {
            super.oauth2();
            views = "apply/wap/apply";
        }

       List<Distributor> distributorList= distributorService.selectDistributorList(new HashMap<String,String>(){{put("code",distributorCode);}});
        if(distributorList!=null && distributorList.size()==1){
            mov.addObject("distributor", distributorList.get(0));
        }
        mov.addObject("distributorCode", distributorCode);
        mov.setViewName(views);
        return mov;
    }

    @RequestMapping(value = "/getApplyWatchById", method = RequestMethod.POST)
    @ResponseBody
    public String getApplyWatchById(Long orderId) {
        ResultObj resultObj = new ResultObj();
        try {
            ApplyWatch applyWatch = (ApplyWatch) applyWatchService.selectApplyByOrderId(orderId);
            resultObj.setData(applyWatch);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj.toJson();
    }


    @RequestMapping(value = "/applyWatch", method = RequestMethod.POST)
    @ResponseBody
    public String applyWatch(ApplyWatch applyWatch, String productEntityIds, String activityIds, Byte orderSourceId, String code) {
        ResultObj resultObj = new ResultObj();
        try {
            String phone = applyWatch.getPhone();
            String randomCode = (String) redisCache.get(phone);
            logger.info(randomCode + "<===============>" + code);
            if (!code.equals(randomCode)) {
                throw new MyRunTimeExcption("验证码错！");
            }
            User user = super.checkUser(applyWatch.getPhone(), applyWatch.getName());
            super.addWechatUser(getOpenId(), user, UserThird.TYPE_WEHCHAT);
            applyWatch.setUserId(user.getTid());

            List<Long> productEntityIdList = new ArrayList<>();
            if (StringUtils.isBlank(productEntityIds)) {
                throw new MyRunTimeExcption("没有找到产品！");
            }
            for (String productEntityId : productEntityIds.split(",")
                    ) {
                productEntityIdList.add(Long.parseLong(productEntityId));
            }
            List<Long> activityIdList = new ArrayList<>();
            if (StringUtils.isNotBlank(activityIds)) {

                for (String activityId : activityIds.split(",")
                        ) {
                    activityIdList.add(Long.parseLong(activityId));
                }
            }
            logger.info(activityIds);
            Order order = orderService.createOrder(productEntityIdList, user.getTid(), activityIdList, orderSourceId);
            applyWatch.setOrderId(order.getOrderId());
            applyWatchService.apply(applyWatch);
            resultObj.setData(order);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj.toJson();
    }
}




